package com.app.shop.activitys;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.app.library.base.BaseActivity;
import com.app.library.beans.EventMessage;
import com.app.library.beans.UserBean;
import com.app.library.utils.SharedPreferencesUtil;
import com.app.library.utils.StringUtil;
import com.app.library.utils.ToastUtil;
import com.app.library.utils.UserManager;
import com.app.shop.R;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class  ModifyActivity extends BaseActivity implements View.OnClickListener{


  private   TextView tvTitle;

    private  EditText edtContent;

    private ImageView imgvreturn;
    private  TextView tvmodify;
    int from = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        edtContent= (EditText) findViewById(R.id.edt_content);
        imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        tvmodify= (TextView) findViewById(R.id.tv_modify);
        imgvreturn.setOnClickListener(this::onClick);
        tvmodify.setOnClickListener(this::onClick);

        initData();
    }

    private void initData() {
        from = getIntent().getExtras().getInt("from", 0);
        if (from == 0) {
            tvTitle.setText("修改昵称");
            edtContent.setText(UserManager.getUserName(this));
            edtContent.setHint("请输入昵称");
        } else if (from == 1) {
            tvTitle.setText("修改手机号");
            edtContent.setText(UserManager.getMobile(this));
            edtContent.setHint("请输入手机号");
        }
    }

    private void save() {
        String content = edtContent.getText().toString();
        if (StringUtil.isEmpty(content)) {
            ToastUtil.showToast(this, "请输入内容");
            return;
        }

        ContentValues values = new ContentValues();
        if (from == 0) {
            values.put("name", content);
            SharedPreferencesUtil.saveData(this, "user", "name", content);
        } else if (from == 1) {
            values.put("mobile", content);
            SharedPreferencesUtil.saveData(this, "user", "mobile", content);

        }
        DataSupport.updateAll(UserBean.class, values, "user_id=?", UserManager.getUserId(this));

        EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
        ToastUtil.showToast(this, "修改成功");
        onBackPressed();
    }




    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
            case R.id.tv_modify:
                save();
                break;
        }
    }
}
